package com.forum.controller;

import com.forum.bean.Articles;
import com.forum.common.Result;
import com.forum.dto.ArticleCreateRequest;
import com.forum.dto.ArticleDetailDto;
import com.forum.dto.ArticleListDto;
import com.forum.dto.LikeRequest;
import com.forum.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping
    public Result<?> getArticleList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false) String searchTitle,
                                    @RequestParam(required = false) Integer userId,
                                    @RequestParam(required = false) Integer status,
                                    @RequestParam(required = false) Integer typeId
                                    ) {
        return articleService.getArticleList(page, pageSize,searchTitle , userId,status,typeId);
    }

//    推荐文章
    @GetMapping("/recommend")
    public Result<?> getRecommendArticleList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "5") Integer size,
                                    @RequestParam(required = false) String searchTitle,
                                    @RequestParam(required = false) Integer userId,
                                    @RequestParam(required = false) Integer status
                                    ) {
        return articleService.getRecommendArticleList(page, size,searchTitle , userId,status);
    }

    /**
     * 查询文章详情接口
     * @param articleId 文章ID，路径变量
     * @param userId 当前登录用户ID，可从请求参数中获取（实际场景建议从token中获取）
     * @return 文章详情及作者、评论信息
     */
    @GetMapping("/{id}")
    public Result<?> getDetail(@PathVariable("id") Integer articleId,
                                                      @RequestParam("userId") Integer userId) {
        return articleService.getArticleDetail(articleId, userId);
    }

    /**
     * POST /api/articles
     * 写文章接口：前端传递标题、内容、用户ID
     */
    @PostMapping("")
    public Result<Articles> createArticle(@RequestBody ArticleCreateRequest request) {
        return articleService.createArticle(request);
    }

    /**
     * 点赞/取消点赞接口，传入文章ID和用户ID后自动切换点赞状态
     */
    @PostMapping("/like")
    public Result<String> toggleLike(@RequestBody LikeRequest req) {
        return articleService.toggleLike(req);
    }


    @DeleteMapping("/{id}")
    public Result<String> deleteArticle(@PathVariable("id") Integer articleId) {
        return articleService.deleteArticle(articleId);
    }
}